//
//  CEMainGoldVC.m
//  digitalCurrency
//
//  Created by 来迎宾 on 2019/4/4.
//  Copyright © 2019年 XinHuoKeJi. All rights reserved.
//

#import "CEMainGoldVC.h"
#import "CEGoldHeaderView.h"
#import "CERuleVC.h"
#import "HomeNetManager.h"
#import "CEPointRaceModel.h"

@interface CEMainGoldVC ()

@property (nonatomic,strong) UIView *sectionView;
@property (nonatomic,strong) CEGoldHeaderView *headerview;
@property (nonatomic,strong) NSMutableArray *goldList;
@property (nonatomic,strong) NSMutableDictionary *goldListParas;

// 我的排名
@property (nonatomic, strong) UILabel *mybangLabel;

@end

@implementation CEMainGoldVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewTitle.text = LocalizationKey(@"Node race");
    
    self.goldList = [NSMutableArray array];
    self.goldListParas = [NSMutableDictionary dictionary];
    
    self.tableView.frame = CGRectMake(15, 0, SCREEN_WIDTH-30, SCREEN_HEIGHT-NAVIGATION_HEIGHT_S);
    [self.view addSubview:self.tableView];
    [self setTablewViewHeard];
    
    [self headRefreshWithScrollerView:self.tableView];

    [self getBonusesInfo];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewWillLayoutSubviews {
//    CGFloat statusHeight = kStatusBarHeight;
//    rect.origin.y += statusHeight;
//    rect.size.height -= statusHeight;
//    CGRect rect = self.view.frame;
//    self.tableView.frame = rect;
}

//MARK:--下拉刷新
- (void)refreshHeaderAction {
    [self getBonusesInfo];
}

- (void)getBonusesInfo {
    [self getBonusesInfo:@{@"userId":[YLUserInfo shareUserInfo].ID}];
}

-(void)setTablewViewHeard {
    
    CEGoldHeaderView *headerview = [[CEGoldHeaderView alloc]initWithFrame:CGRectMake(0, 0, kWindowW,  265)];
    self.tableView.rowHeight = 44;
    self.headerview = headerview;
    self.tableView.tableHeaderView = headerview ;
    
    self.tableView.separatorColor  = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor blackColor];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _goldList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor colorWithHEX:@"282f3a"];
        cell.textLabel.font  = cell.detailTextLabel.font
                             = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor       = [UIColor whiteColor];
        cell.detailTextLabel.textColor = [UIColor whiteColor];
    }
    top10UserInfo *infoModel = self.goldList[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%2ld   %@",(long)indexPath.row + 1,[self handlePhone:infoModel.memberAccount]];
    cell.detailTextLabel.text = [ToolUtil judgeStringForDecimalPlaces:infoModel.asset point:4];
    
    return cell;
}
- (NSString *)handlePhone:(NSString *)number {
    if (number.length <= 0) {
        return number;
    }
    
    if (number.length < 7) {
        return number;
    }
    
    return [number stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)pushvc {
    CERuleVC *rule = [[CERuleVC alloc] init];
    [self.navigationController pushViewController:rule animated:YES];
}

//CE/invest/getBonusesInfo
- (void)getBonusesInfo:(NSDictionary *)paras {
    [HomeNetManager getBonusesInfo:paras CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            [self.goldList removeAllObjects];
            
            NSInteger codeInt  = [resPonseObj[@"code"] integerValue];
            NSDictionary *data = resPonseObj[@"data"];
            CEPointRaceModel *raceModel = [CEPointRaceModel mj_objectWithKeyValues:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.headerview.allcountLabel.text = [NSString stringWithFormat:@"%@ DWTT",[ToolUtil judgeStringForDecimalPlaces:raceModel.allBonuseAsset point:4]];
                self.headerview.goldcoinLabel.text = [NSString stringWithFormat:@"%@%@",LocalizationKey(@"Last week champion:"),raceModel.championAccount.length == 0 ? LocalizationKey(@"No") : raceModel.championAccount];
                self.headerview.lastweakLabel.text = [NSString stringWithFormat:@"%@%@ DWTT",LocalizationKey(@"Champion bonus"),[ToolUtil judgeStringForDecimalPlaces:raceModel.championBonuse point:4]];
                self.mybangLabel.text = [NSString stringWithFormat:@"%@%@",LocalizationKey(@"My ranking"),[raceModel.myIndex integerValue]  == -1 ?  LocalizationKey(@"Not listed") : raceModel.myIndex];
            });
            
            NSArray *top10 = data[@"top10UserInfo"];
            if (codeInt == 0) {
                for (NSDictionary *subdic in top10) {
                    top10UserInfo *infoModel = [top10UserInfo mj_objectWithKeyValues:subdic];
                    [self.goldList addObject:infoModel];
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            }
            else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
                });
            }
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
            });
        }
    }];
}

#pragma mark-自定义section头部的高度
- (UIView *)sectionView {
    if (!_sectionView) {
        CGFloat viewwidth = SCREEN_WIDTH;
        _sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewwidth, 100)];
        _sectionView.backgroundColor = [UIColor blackColor];
        UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, 105)];
        bgview.layer.masksToBounds = YES;
        bgview.layer.cornerRadius = 5.0;
        bgview.backgroundColor = [UIColor colorWithHEX:@"282f3a"];
        [_sectionView addSubview:bgview];
        
        UILabel *mybangLabel  = [[UILabel alloc] init];
        mybangLabel.text      = LocalizationKey(@"My ranking");
        mybangLabel.textColor = [UIColor whiteColor];
        mybangLabel.font = [UIFont systemFontOfSize:14];
        [bgview addSubview:mybangLabel];
        self.mybangLabel = mybangLabel;
        [mybangLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(15);
            make.width.mas_equalTo(viewwidth-120);
            make.height.mas_equalTo(20);
        }];
        
        UIButton *ruleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ruleBtn setTitle:[NSString stringWithFormat:@"%@ >",LocalizationKey(@"Reward rules")] forState:UIControlStateNormal];
        [ruleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        ruleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [bgview addSubview:ruleBtn];
        [ruleBtn addTarget:self action:@selector(pushvc) forControlEvents:UIControlEventTouchUpInside];
        [ruleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(10);
            make.height.mas_equalTo(30);
        }];
        
        UIView *line = [[UIView alloc] init];
        [bgview addSubview:line];
        line.backgroundColor = [UIColor whiteColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(50);
            make.left.mas_equalTo(15);
            make.height.mas_equalTo(1);
        }];
        
        UILabel *l1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 70, viewwidth/2.0 - 15, 15)];
        l1.text = LocalizationKey(@"Top ten this week");
        l1.textColor = [UIColor whiteColor];
        l1.font = [UIFont systemFontOfSize:15];
        [bgview addSubview:l1];
        
        UILabel *l2 = [[UILabel alloc] initWithFrame:CGRectMake(viewwidth/2.0, 70, viewwidth/2.0-45, 15)];
        l2.text = LocalizationKey(@"Performance (CT)");
        l2.textColor = [UIColor whiteColor];
        l2.font = [UIFont systemFontOfSize:15];
        l2.textAlignment = NSTextAlignmentRight;
        [bgview addSubview:l2];
    }
    
    return _sectionView;
}


@end
